<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" /> 
        <title>Vue检测数据改变的原理</title>
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h1>学生信息</h1>

            <button @click="student.age++">年龄+1岁</button><br/>
            <button @click="addSex">添加性别属性，默认值：男</button><br/>
            <button @click="student.sex = '未知'">修改性别</button><br/>

            <button @click="addFriend">在列表首位添加一个朋友</button><br/>
            <button @click="updateFirstFriendName">修改第一个朋友的名字为：张三</button><br/>
            <button @click="addHobby">添加一个爱好</button><br/>
            <button @click="updateFirstHobbyName">修改第一个爱好为：开车</button><br/>

            <h3>姓名: {{student.name}}</h3>
            <h3 v-show="student.sex">性别: {{student.sex}}</h3>
            <h3>年龄: {{student.age}}</h3>
            <h3>爱好 </h2>
            <ul>
                <li v-for="(h,index) in student.hobby" :key="index">
                    {{h}}
                </li>
            </ul>
            <h2>朋友们 </h2>
            <ul>
                <li v-for="(f,index) in student.friends" :key="index">
                    {{f.name}} -- {{f.age}}
                </li>
            </ul>
        </div>
    </body>
    <script type="text/javascript">
        Vue.config.productionTip = false // 阻止vue在启动时生成生产提示。

        const vm = new Vue({
            el:'#root',
            data:{
                student:{
                    name: 'tom',
                    age: 18,
                    hobby:['smoke', 'drink', 'make hairs'],
                    friends:[
                        {name: 'jerry', age:35},
                        {name: 'tony', age:36},
                    ]
                }
            },
            methods:{
                addSex(){
                    Vue.set(this.student,'sex','男');
                    // this.$set(this.student,'sex','男');
                },
                addFriend(){
                    this.student.friends.unshift({name:'lily', age:18})
                },
                updateFirstFriendName(){
                    this.student.friends[0].name = 'zhangsan';
                },
                addHobby(){
                    this.student.hobby.push('newhobby')
                },
                updateFirstHobbyName(){
                    this.student.hobby.splice(0,1,'开车');
                    Vue.set(this.student.hobby,0,'开车');
                }
            }
        })
    </script>
</html>